package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"sens-fix/cache"
	"sens-fix/model"
	"strconv"
	"time"
)

func BigCarAdd(c *gin.Context) {
	carON := c.PostForm("carON")
	carName := c.PostForm("carName")
	carPrice, _ := strconv.Atoi(c.PostForm("carPrice"))
	tid, _ := strconv.Atoi(c.PostForm("tid"))
	data := model.BigCar{
		CarON:    carON,
		CarName:  carName,
		CarPrice: carPrice,
		Tid:      tid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func BigCarTypeAdd(c *gin.Context) {
	typeNo := c.PostForm("typeNo")
	typeName := c.PostForm("typeName")
	typeDesc := c.PostForm("typeDesc")
	data := model.BigCatType{
		TypeNo:   typeNo,
		TypeName: typeName,
		TypeDesc: typeDesc,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func BigCarOrderAdd(c *gin.Context) {
	orderON := c.PostForm("orderON")
	orderFrom := c.PostForm("orderFrom")
	orderTo := c.PostForm("orderTo")
	orderGood := c.PostForm("orderGood")
	bid, _ := strconv.Atoi(c.PostForm("bid"))
	data := model.BigCarOrder{
		OrderON:   orderON,
		OrderFrom: orderFrom,
		OrderTo:   orderTo,
		OrderGood: orderGood,
		Bid:       bid,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func BigCarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  big_car_orders.id AS OrderId,
		  big_cars.car_on AS CarON,
		  big_cars.car_name AS CarName,
		  big_cars.car_type AS CarType,
		  big_cars.car_price AS CarPrice,
		  big_car_orders.order_on AS OrderON,
		  big_car_orders.order_from AS OrderFrom,
		  big_car_orders.order_to AS OrderTo,
		  big_car_orders.order_good AS OrderGood
		FROM
		  big_car_orders
		  LEFT JOIN big_cars ON big_cars.id = big_car_orders.bid
	*/
	var list []model.BigCarOrderList
	redis_key := "big_order_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("big_car_orders").
			Select(
				"big_car_orders.id AS OrderId",
				"big_cars.car_on AS CarON",
				"big_cars.car_name AS CarName",
				"big_cars.car_type AS CarType",
				"big_cars.car_price AS CarPrice",
				"big_car_orders.order_on AS OrderON",
				"big_car_orders.order_from AS OrderFrom",
				"big_car_orders.order_to AS OrderTo",
				"big_car_orders.order_good AS OrderGood",
			).
			Joins("LEFT JOIN big_cars ON big_cars.id = big_car_orders.bid").
			Offset(offset).Limit(size).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}

func BigCarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  big_car_orders.id AS OrderId,
		  big_cars.car_on AS CarON,
		  big_cars.car_name AS CarName,
		  big_cars.car_type AS CarType,
		  big_cars.car_price AS CarPrice,
		  big_car_orders.order_on AS OrderON,
		  big_car_orders.order_from AS OrderFrom,
		  big_car_orders.order_to AS OrderTo,
		  big_car_orders.order_good AS OrderGood
		FROM
		  big_car_orders
		  LEFT JOIN big_cars ON big_cars.id = big_car_orders.bid
	*/
	var detail model.BigCarOrderList
	redis_key := "big_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("big_car_orders").
			Select(
				"big_car_orders.id AS OrderId",
				"big_cars.car_on AS CarON",
				"big_cars.car_name AS CarName",
				"big_cars.car_type AS CarType",
				"big_cars.car_price AS CarPrice",
				"big_car_orders.order_on AS OrderON",
				"big_car_orders.order_from AS OrderFrom",
				"big_car_orders.order_to AS OrderTo",
				"big_car_orders.order_good AS OrderGood",
			).
			Joins("LEFT JOIN big_cars ON big_cars.id = big_car_orders.bid").
			Where("big_car_orders.id = ?", id).Find(&detail)
		jsonData, _ := json.Marshal(detail)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": detail,
	})
	return
}

func BigCarTypeList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
			SELECT
		  big_cars.id AS Cid,
		  big_cars.car_on AS CarON,
		  big_cars.car_name AS CarName,
		  big_cars.car_price AS CarPrice,
		  big_cat_types.type_no AS TypeNo,
		  big_cat_types.type_name AS TypeName,
		  big_cat_types.type_desc AS TypeDesc
		FROM
		  big_cars
		  LEFT JOIN big_cat_types ON big_cat_types.id = big_cars.tid
	*/
	var list []model.BigCarTypeList
	redis_key := "big_order_lists" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("big_cars").
			Select(
				"big_cars.id AS Cid",
				"big_cars.car_on AS CarON",
				"big_cars.car_name AS CarName",
				"big_cars.car_price AS CarPrice",
				"big_cat_types.type_no AS TypeNo",
				"big_cat_types.type_name AS TypeName",
				"big_cat_types.type_desc AS TypeDesc",
			).
			Joins("LEFT JOIN big_cat_types ON big_cat_types.id = big_cars.tid").
			Offset(offset).Limit(size).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": list,
	})
	return
}

func BigCarTypeDetail(c *gin.Context) {
	id := c.Query("id")
	var detail model.BigCarTypeList
	redis_key := "big_type_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("big_cars").
			Select(
				"big_cars.id AS Cid",
				"big_cars.car_on AS CarON",
				"big_cars.car_name AS CarName",
				"big_cars.car_price AS CarPrice",
				"big_cat_types.type_no AS TypeNo",
				"big_cat_types.type_name AS TypeName",
				"big_cat_types.type_desc AS TypeDesc",
			).
			Joins("LEFT JOIN big_cat_types ON big_cat_types.id = big_cars.tid").
			Where("big_cars.id = ?", id).Find(&detail)
		jsonData, _ := json.Marshal(detail)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &detail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": detail,
	})
	return
}
